package aaBaolilo;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import aaaBrowser.ClosureEmpty;
import aaaBrowser.atiBrowserExample;

import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.ClipboardUtil;

public class LoginAndPub {

	public static void main(String[] args) {
		new LoginAndPub().start();
	}

	final Display display = new Display();
	atiBrowserExample atiBrowserExampleinstance;

	private void start() {

		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("Browser example");
		// shell.setVisible(false);
		// shell.setSize(1, 1);
		atiBrowserExampleinstance = new atiBrowserExample(shell);

		shell.open(); // shell.setVisible(false);

		// core.execMeth_Ays(new Runnable() {
		//
		// @Override
		// public void run() {
		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");
		// atiBrowserExampleinstance
		// .gotoUrl("http://demo.destoon.com/v5.0/member/login.php?a0");
		//
		// }
		// }, "--");

		login();

		// atiBrowserExampleinstance.ini();

		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		// icon.dispose();
		atiBrowserExampleinstance.dispose();
		display.dispose();
	}

	/**
	 * 账号100719000050 ，密码065355，网址http://fxs.gzmcc.net/Login.aspx
	 */
	private void login() {
		core.execMeth_Ays(new Runnable() {

			@Override
			public void run() {
				System.out.println("=-ajsy");
				display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {

						// atiBrowserExampleinstance
						// .gotoUrl("http://www.baidu.com");
						atiBrowserExampleinstance
								.gotoUrl("http://fxs.gzmcc.net/Login.aspx");
						atiBrowserExampleinstance
								.addProgressListener(
										pathx.classPath(LoginAndPub.class)
												+ "\\login.js", new Closure() {

											@Override
											public Object execute(Object arg0)
													throws Exception {
												chargeCardWait();
												return null;
											}

										

										} );
						
				
						atiBrowserExampleinstance.regJsCallbackMeth(
								"getCaptch", new Closure() {

									@Override
									public Object execute(Object arg0)
											throws Exception {
										setCaptch();
										return null;
									}
								});
						// atiBrowserExampleinstance.regJsCallbackMeth(
						// "gotoChargeCard", new Closure() {
						//
						// @Override
						// public Object execute(Object arg0)
						// throws Exception {
						// System.out.println("----gotoChargeCard from js invok ");
						// chargeCard();
						// return null;
						// }
						// });
						//

						atiBrowserExampleinstance.gotoUrl();
						
//						execMeth_AysGui(new   Runnable() {
//							public void run() {
////								try {
////									Thread.sleep(7000);
////								} catch (InterruptedException e) {
////									// TODO Auto-generated catch block
////									e.printStackTrace();
////								}
//								atiBrowserExampleinstance.	browser.addLocationListener(new LocationListener() {
//									public void changed(LocationEvent event) {
//									//	busy = true;
//										// if (event.top)
//										// location.setText(event.location);
//									//	System.out.println("--   url changeed event");
//								//		System.out.println(event.toString());
//									}
//
//									public void changing(LocationEvent event) {
//									}
//								});
//							}
//						}, "");
						
						// atiBrowserExampleinstance.
						// display.getDefault().asyncExec(new Runnable(){
						//
						// @Override
						// public void run() {
						// atiBrowserExampleinstance
						// .gotoUrl();
						//
						// }});

						// atiBrowserExampleinstance.waitFinish(display);

					}
				});

			}
		}, "");
	}
	private void chargeCardWait() {
		atiBrowserExampleinstance
		.addProgressListener(
				pathx.classPath(LoginAndPub.class)
						+ "\\empty.js", new Closure() {

					@Override
					public Object execute(Object arg0)
							throws Exception {
						System.out.println("--chargeCardWait event");
					//	System.out.println("-- satrt chargeCard()");
						chargeCard();
						return null;
					}

				

				} );

	}
	private void chargeCard() {
		// try {
		// Thread.sleep(15000);
		// } catch (InterruptedException e1) {
		// // TODO Auto-generated catch block
		// e1.printStackTrace();
		// }
		execMeth_AysGui(new Runnable() {

			@Override
			public void run() {

//				try {
//					Thread.sleep(15000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}

//				while (true) {
//					try {
//						Thread.sleep(3000);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					String html = atiBrowserExampleinstance.browser.getText();
//					if (html.contains("补换卡"))
//						break;
//					else
//						System.out.println(" not contain buhweka ");
//
//				}
				System.out.println("---start charge card jensh evenrt");
				atiBrowserExampleinstance.gotoUrl("http://fxs.gzmcc.net/Modules/BusinessAgency/ChangingCard.aspx");
				atiBrowserExampleinstance.addProgressListener(
						pathx.classPath(LoginAndPub.class) + "\\chargeCard.js",
						new Closure() {

							@Override
							public Object execute(Object arg0) throws Exception {
								return null;
							}

						});

				atiBrowserExampleinstance.gotoUrl();

			}
		}, "");

	}

	private void setCaptch() {
		System.out.println("aaaabbbcccdddd00000aa");
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();// 得到系统的剪贴板
		// 获取粘贴板图片
		Image image = null;
		try {
			image = ClipboardUtil.getImageFromClipboard();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String basename = filex.getUUidName();
		String jpg = "c:\\" + basename + ".jpg";
		System.out.println(jpg);
		// image.get
		File file = new File(jpg);
		// 转成jpg
		BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
				image.getHeight(null), BufferedImage.TYPE_INT_RGB);
		// 转成png
		// BufferedImage bufferedImage = new BufferedImage(image.getWidth(null),
		// image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = bufferedImage.createGraphics();
		g.drawImage(image, null, null);
		// ImageIO.write((RenderedImage)bufferedImage, "jpg", file);
		try {
			ImageIO.write((RenderedImage) bufferedImage, "jpg", file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String deboxJpg = "c:\\" + basename + "_deBox.jpg";
		String bin_jpg = "c:\\" + basename + "_bin.jpg";
		try {
			Thumbnails.of(jpg)
			// 从原图哪里开始裁剪 裁减多少
					.sourceRegion(Positions.CENTER, 50, 23)
					// 新图的大小
					.size(50, 23).toFile(deboxJpg);

			ImageDemo demo = new ImageDemo();

			demo.binaryImage(deboxJpg, bin_jpg);
			// Thumbnails.of(jpg)
			// //从原图哪里开始裁剪 裁减多少
			// .sourceRegion(Positions.TOP_LEFT, 400, 400)
			// //新图的大小
			// .size(50, 23).toFile(deboxJpg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String captchCode = OcrX.getCaptch(bin_jpg);
		String js = filex.read(pathx.classPath(LoginAndPub.class)
				+ "\\set_captch.js");
		js = js.replaceAll("@capt", captchCode);
		// "1234";
		atiBrowserExampleinstance.browser.execute(js);

		// atiBrowserExampleinstance.browser.execute(script);
		// core.execMeth_Ays(new Runnable() {
		//
		// @Override
		// public void run() {
		// System.out.println(" start pub");
		// try {
		// Thread.sleep(8000);
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// display.getDefault().asyncExec(new Runnable() {
		//
		// @Override
		// public void run() {
		//
		// atiBrowserExampleinstance.gotoUrl("http://localhost/destoon/member/my.php?mid=5&action=add&aP4=java");
		// atiBrowserExampleinstance.addProgressListener(
		// pathx.classPath(LoginAndPub.class)
		// + "\\pub.js", new ClosureEmpty());
		// atiBrowserExampleinstance.gotoUrl();
		// System.out.println(" start pub finish");
		// }
		// });
		// }
		// }, "--");

	}

	private void execMeth_AysGui(final Runnable runnable, String string) {
		core.execMeth_Ays(new Runnable() {

			@SuppressWarnings("static-access")
			@Override
			public void run() {
				System.out.println("=-ajsy");
				// Display default1 = display.getDefault();
				// runnable.run();
				Display.getDefault().asyncExec(runnable);
				// display.asyncExec(runnable);
			}
		}, string);

	}

}
